package com.dog.basics.netty.business;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.dog.basics.netty.business.impl.*;
import com.dog.basics.netty.pojo.BusinessBean;
import com.dog.basics.netty.protobuf.dto.DogNewsContentDto;
import com.dog.basics.netty.util.ConstantUtil;
import com.dog.basics.netty.util.NettyAttrUtil;
import com.dog.basics.netty.util.SessionSocketHolder;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.log4j.Log4j2;

import java.util.Map;

/**
 * 业务工厂，实例化业务
 */
@Log4j2
public class BusinessFactory {


    /**
     * 业务实现方法类
     */
    public static Map<Integer, Class<? extends IBusinessServer>> businessMap = CollUtil.newHashMap();


    static {
        /**
         * 未知请求响应
         */
        businessMap.put(ConstantUtil.UNKNOWN, ResponseHandlerImplUnknown.class);
        /**
         * 未注册请求响应
         */
        businessMap.put(ConstantUtil.UNREGISTERED, ResponseHandlerImplUnregistered.class);
        /**
         * 登录请求
         */
        businessMap.put(ConstantUtil.LOGON, ResponseHandlerImplLogin.class);
        /**
         * 心跳请求
         */
        businessMap.put(ConstantUtil.HEARTBEAT, ResponseHandlerImplKeepAlive.class);
        /**
         * 固件升级文件请求
         */
        businessMap.put(ConstantUtil.FIRMWAREFILE, ResponseHandlerImplFirmwareFile.class);
        /**
         * 异常上报
         */
        businessMap.put(ConstantUtil.ABNORMALREPORT, ResponseHandlerImplAbnormalReport.class);
        /**
         * 远程在线升级回应
         */
        businessMap.put(ConstantUtil.REMOTEUPGRADE, ResponseHandlerImplRemoteUpgrade.class);
        /**
         * 远程重启
         */
        businessMap.put(ConstantUtil.REMOTEREBOOT, ResponseHandlerImplRemoteReboot.class);
        /**
         * 远程关机
         */
        businessMap.put(ConstantUtil.REMOTESHUTDOWN, ResponseHandlerImplRemoteShutdown.class);
        /**
         * 远程自定义控制
         */
        businessMap.put(ConstantUtil.REMOTECUSTOMCONTROL, ResponseHandlerImplRemoteCustomControl.class);
        /**
         * 归还轮椅（关锁）
         */
        businessMap.put(ConstantUtil.RETURNWHEELCHAIR, ResponseHandlerImplReturnWheelchair.class);
        /**
         *  租赁轮椅（开锁）
         */
        businessMap.put(ConstantUtil.RENTINGAWHEELCHAIR, ResponseHandlerImplRentingAWheelchair.class);
        /**
         * 轮椅异常上报 (锁异常)
         */
        businessMap.put(ConstantUtil.WHEELCHAIREX, ResponseHandlerImplWheelchairEx.class);
        /**
         * 开锁测试
         */
        businessMap.put(ConstantUtil.UNLOCKINGTEST, ResponseHandlerImplUnlockingTest.class);

    }


    /**
     * 调用执行业务
     *
     * @param businessBean
     * @throws InterruptedException
     */
    public static Boolean goOn(BusinessBean businessBean) {
        return BusinessFactory.offer(businessBean);
    }

    /**
     * 执行业务
     *
     * @param businessBean
     * @return
     */
    public static Boolean offer(BusinessBean businessBean) {
        try {
            if (businessBean == null) {
                return Boolean.FALSE;
            }
            DogNewsContentDto.Message msg = businessBean.getMsg();
            ChannelHandlerContext ctx = businessBean.getCtx();

            Class<? extends IBusinessServer> cls = null;
            // 判断什么消息类型
            switch (msg.getMsgType()) {
                case ConstantUtil.LOGON:                // 登录请求
                    cls = businessMap.get(ConstantUtil.LOGON);
                    break;
                case ConstantUtil.HEARTBEAT:            // 心跳请求
                case ConstantUtil.FIRMWAREFILE:         // 获取固件升级文件
                case ConstantUtil.ABNORMALREPORT:       // 异常上报
                case ConstantUtil.REMOTEUPGRADE:        // 远程在线升级
                case ConstantUtil.REMOTEREBOOT:         // 远程重启
                case ConstantUtil.REMOTESHUTDOWN:       // 远程关机
                case ConstantUtil.REMOTECUSTOMCONTROL:  // 远程自定控制指令
                case ConstantUtil.RETURNWHEELCHAIR:    // 归还轮椅
                case ConstantUtil.RENTINGAWHEELCHAIR:  // 租赁轮椅
                case ConstantUtil.BLUETOOTHRETURNWHEELCHAIR:   // 蓝牙归还轮椅
                case ConstantUtil.BLUETOOTHRENTINGAWHEELCHAIR: // 蓝牙租赁轮椅
                case ConstantUtil.WHEELCHAIREX:       // 轮椅异常上报
                case ConstantUtil.UNLOCKINGTEST:       // 开锁测试
                case ConstantUtil.GETLOCKSTATE:       // 查询轮椅状态
                    // 判断是否有登录记录
                    if (ObjectUtil.isNotNull(SessionSocketHolder.getSession(ctx.channel().id().asLongText()))) {
                        cls = businessMap.get(msg.getMsgType());
                    } else {
                        // 未注册登录请求响应
                        cls = businessMap.get(ConstantUtil.UNREGISTERED);
                    }
                    break;
                default:                                 // 未知请求
                    // 未知请求响应 归还轮椅
                    cls = businessMap.get(ConstantUtil.UNKNOWN);
                    break;
            }

            // 未找到实现方法则返回
            if (cls == null) {
                cls = businessMap.get(ConstantUtil.UNKNOWN);
            }
            // 访问 - 计时 （超时）
            NettyAttrUtil.updateReaderTime(ctx.channel(), DateUtil.currentSeconds());
            // 实例化类
            IBusinessServer iBusinessServer = cls.newInstance();
            // 执行业务方法
            iBusinessServer.businessHandler(ctx, msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Boolean.TRUE;
    }
}
